/**
 * @Description:
 * @author 紫霞
 * @since 2019/1/17
 */
// React基础组件
import React from 'react'
// dva 连接组件 - 连接route和model
import { connect } from 'dva'
// 引入antd的组件
import { Row, Col, Card, Button, Tabs, Form, Popconfirm, Modal, } from 'antd'
//导入路由组件，用于跳转等
import { Link, hashHistory } from 'dva/router'
// 导入项目全局配置文件
import config from 'config/config'

// 引入model取值工具方法
import { mapStateToProps } from 'utils/view_utils'
//引用listPage
import ListPage from 'widget/PageTmpl/ListPage'
// 当前组件样式
import styles from './intermediaryApplyListStyle.less'
import { Grid, QueryCondition } from 'widget/Grid/index'
import { AdvanceSearch } from 'widget/CollapsiblePanels/index'
import Keypress  from 'widget/Keypress'
import { chunk } from 'lodash'

import classnames from 'classnames/bind';
//权限管理
import FlagBtn from 'widget/FlagBtn/FlagView'
let cx = classnames.bind(styles);

/**
 * description 路由视图及事件处理
 * dispatch 分发到models中的effects对应事件名
 * model 用于获取models中state定义的数据
 * form 表单对象
 */
const TabPane = Tabs.TabPane
const routeView = function ({dispatch, model, form}) {
  // 表单的子函数
  const {resetFields, getFieldDecorator, validateFields, setFieldsValue} = form
  // 取出model对象，命名空间
  const {modelObj, namespace} = model
  let {loading, btnLoading, modalObj, currentItem, queryList, tableData, pageConfig,statisticsInfo} = modelObj
  // modelObj.xxx获取model中状态机的值
  //console.log('modelObj', modelObj)
  // 点击处理事件
  const clickHandler = (e) => {
    // 分发到model文件中effects的xxx方法
    dispatch({
      type: `${namespace}/xxx`,
      payload: {}
    })
  }
  
  let statusMap = {
    1: {
      value: '1',
      label: '启用'
    },
    2: {
      value: '2',
      label: '禁用'
    },
  }
  //  信用代码 中介公司名称  合作楼盘  联系人 联系电话 备注
  const columns = [
    {title: '信用代码', dataIndex: 'orgCode', key: 'orgCode',},
    {title: '申请合作时间', dataIndex: 'createTime', key: 'createTime',width:180},
    {title: '中介公司名称', dataIndex: 'orgName', key: 'orgName'},
    {title: '合作楼盘', dataIndex: 'productName', key: 'productName'},
    {title: '联系人', dataIndex: 'name', key: 'name',width:80},
    {title: '联系电话', dataIndex: 'mobile', key: 'mobile',width:130},
    {title: '备注', dataIndex: 'remark', key: 'remark'},
  ]

  // 表格模板配置
  const gridProps = {
    nameSpace: namespace,
    data: {
      // columns: columns,
      columns: [].concat(columns, generateAction()),
      pageConfig, tableData
    },
    pageChange: (pageNo, pageSize) => {
      console.log(pageNo, pageSize)
      dispatch({ type: `${namespace}/pageChange`, payload: {pageNo, pageSize } });
    },
    scroll: {x: 800},
    loading
  }

  // 表格操作区域 备案列表  启用  停用
  function generateAction () {
    return {
      title: '操作',
      key: 'operation',
      fixed: 'right',
      width: 200,
      render: (data, item, index) => {
        return (
          <div>
            <span>
              <a onClick={() =>{routerGo({url:'/home/channelAdmin/reportClientList',query:{orgId:item.orgId,agentConfigId:item.agentConfigId}})}}>查看</a>{<span className="ant-divider"/>}
            </span>
            {
            <span>
              <a onClick={() =>{routerGo({url:'/home/channelAdmin/intermediaryDetail',query:{pageType:'edit',orgId:item.orgId,agentConfigId:item.agentConfigId}})}}>修改</a>{<span className="ant-divider"/>}
            </span>
            }
          </div>
        )
      }
    }
  }

  

  //修改状态
  const changeState = (payload) => {
    dispatch({
      type: `${namespace}/updateStore`,
      payload: {...payload}
    })
  }
  //查询
  const onSearch = (payload) => {
    dispatch({
      type: `${namespace}/search`,
      payload: payload
    })
  }
  // 详情页跳转
  function routerGo ({url='', query={} }) {
    hashHistory.push({
      pathname: url,
      query: query
    })
  }

  return (
    <div className={cx('pageLayout')}>
      {/*监控键盘事件，自动刷新数据*/}
      <Keypress keyCode={13} cbk={()=>{
        onSearch();
      }} />
      <div className="public_listMain">
        <div className="functionButton">
        <FlagBtn node={<Button type="primary" loading={modelObj.exportLoading}  onClick={() =>{
            dispatch({
              type: `${namespace}/exportList`,
              payload: {}
            })
          }}>导出</Button>} btnId={'40201'}> </FlagBtn>
          
          {/*<Button type="primary" onClick={() =>{routerGo({url:'/home/channelAdmin/intermediaryDetail',query:{pageType:'add'}})}}>新增</Button>*/}
        </div>
        <AdvanceSearch
          footer={
            <span>
                <Button type="primary" htmlType="submit" onClick={() => onSearch()} loading={modelObj.loading}>搜索</Button>
                <Button onClick={() => dispatch({ type: `${namespace}/queryReset` })}>重置</Button>
              </span>
          }>
          <QueryCondition
            data={queryList}
            inputChange={(key, value) => dispatch({type: `${namespace}/inputChange`, payload: {key, value}})}
          />
        </AdvanceSearch>
        {/* 表格区域 */}
        <div className="boxShadow">
          {/* 添加用户 */}
          <Grid {...gridProps} />
        </div>
      </div>
    </div>
  )
}

// 连接视图(view)和模型(model)
export default connect(mapStateToProps)(Form.create()(routeView))

